package com.md24housecall.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

public class CustomViewPager extends ViewPager{
//	private int childId;
	private GestureDetector mGestureDetector;
	private boolean mIsLockOnHorizontalAxis = false;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new XScrollDetector());
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
//        if (childId > 0) {          
//            ViewPager pager = (ViewPager)findViewById(childId);
//            if (pager != null) {           
//                pager.getParent().requestDisallowInterceptTouchEvent(true);
//            }
//        }
        getParent().requestDisallowInterceptTouchEvent(true);
        onTouchEvent(event);
        return super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	// TODO Auto-generated method stub
    	if (!mIsLockOnHorizontalAxis)
			mIsLockOnHorizontalAxis = mGestureDetector.onTouchEvent(event);

		// release the lock when finger is up
		if (event.getAction() == MotionEvent.ACTION_UP)
			mIsLockOnHorizontalAxis = false;
//		MyLog.d(GlobalConstant.TAG,"mIsLockOnHorizontalAxis: "+mIsLockOnHorizontalAxis);
//		getParent().requestDisallowInterceptTouchEvent(mIsLockOnHorizontalAxis);
		return super.onTouchEvent(event);
    }
    
    public void setChildId(int id) {
//        this.childId = id;
    }
    
    private class XScrollDetector extends SimpleOnGestureListener {

		// -----------------------------------------------------------------------
		//
		// Methods
		//
		// -----------------------------------------------------------------------
		/**
		 * @return true - if we're scrolling in X direction, false - in Y direction.
		 */
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			return Math.abs(distanceX) > Math.abs(distanceY);
		}

	}
}
